/**
 * 应用基础视图
 */
define(['jquery', 'underscore', 'RootView', 'validation'],
    function ($, _, RootView) {
        var MainView = RootView.extend({
            el: '#sign-body',
            events: {
                'focus                        input': 'focusInput',
                'blur                         input': 'blurInput',
                'keyup        .validate-input input': 'validateCode',
                'click               .validate-code': 'refreshValidateCode'
            },
            initialize: function () {
                this.initializeInputVal();
            },
            
            //页面加载时判断输入框是否有内容，对样式初始化
            initializeInputVal: function () {
                this.$('input').each(function () {
                    inputVal = $(this).val();
                    if (_.isNull(inputVal) || _.isEqual('', inputVal)) {
                        $(this).closest('.input-group').removeClass('expand');
                    } else {
                        $(this).closest('.input-group').addClass('expand');
                    }
                });
            },
            
            //Input获取焦点
            focusInput: function (event) {
                var $target = $(event.target);
                $target.closest('.input-group').addClass('expand');
            },
            
            //Input失去焦点
            blurInput: function (event) {
                var $target = $(event.target),
                    inputVal = $target.val();

                if (_.isNull(inputVal) || _.isEqual('', inputVal)) {
                    $target.closest('.input-group').removeClass('expand');
                }
            },
            
            //验证码
            validateCode: function (event) {
                var $target = $(event.target),
                    $validateInput = $target.closest('.validate-input'),
                    $validateStatus = $validateInput.find('.validate-status'),
                    validateCode = $target.val(),
                    validateName = $target.attr('name'),
                    validateHref = $validateInput.data('validate-href'),
                    data = {},
                    that = this;
                data[validateName] = validateCode;

                if (validateCode.length == 4) {
                    $.ajax({
                        url: validateHref,
                        data: data,
                        type: 'post',
                        dataType: 'json',
                        beforeSend: function (req) {
                            req.setRequestHeader("Accept", "application/json");
                        },
                    }).done(function (res) {
                        if (!res.Content || !res.Errors.IsValid) {
                            if ($validateStatus.hasClass('success')) {
                                $validateStatus.removeClass('success');
                            }
                            if (!$validateStatus.hasClass('error')) {
                                $validateStatus.addClass('error');
                            }
                        } else {
                            if ($validateStatus.hasClass('error')) {
                                $validateStatus.removeClass('error');
                            }
                            if (!$validateStatus.hasClass('success')) {
                                $validateStatus.addClass('success');
                            }
                        }
                    }).fail(function () {

                    });
                } else if (validateCode.length == 0) {
                    if ($validateStatus.hasClass('success')) {
                        $validateStatus.removeClass('success');
                    }
                    if ($validateStatus.hasClass('error')) {
                        $validateStatus.removeClass('error');
                    }
                    return;
                } else {
                    return;
                }


            }

        });
        return MainView;
    });
